<template>
  <div class='box'>
    <div class="login-form pl60">
      <el-form :model="ruleForm" lable-width="0px" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="" prop="phone">
        <el-input type="text" v-model="ruleForm.phone" autocomplete="off"
          placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"
          placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
    </div>
    <div class="footer">
      <a href="" class='fl mt10 other-login-method' @click.prevent='backToLoginList(true)'>&lt; 其他登录方式</a>
      <a href="" class='fr mt10 free-register' @click.prevent='goToRegister(true)'>没有账号?免费注册</a>
    </div>
  </div>
</template>

<script>
import store from '../../store'
import { validatePhoneNumber, validatePassword } from '../../utils/validate'
export default {
  name: 'phoneLogin',
  props: ['showPhoneLogin', 'showLoginList', 'showRegister', 'toogleDialog'],
  data() {
    var validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入手机号'));
        } else if (!validatePhoneNumber(value)) {
          callback(new Error('请输入11位数字的手机号!'));
        } else {
          callback()
        }
    };
    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if (!validatePassword(value)) {
          callback(new Error('请输入8-20个字母、数字、下划线'));
        } else {
          callback()
        }
    };
    return {
        ruleForm: {
          phone: '',
          password: ''
        },
        rules: {
          phone: [
            { validator: validatePhone, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let param = {
            phone: this.ruleForm.phone,
            password: this.ruleForm.password
          }
          let res = await this.$api.phoneLogin(param)
          console.log('手机号登录',res)
          if (res.data.code == 200) {
            store.dispatch('user/getLoginStatus')
            localStorage.setItem("token", res.data.token)
            localStorage.setItem("cookie", res.data.cookie)
            this.$emit('toogleDialog', false, true)
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    backToLoginList(flag) {
      this.$emit('showLoginList', flag)
    },
    goToRegister(flag) {
      this.$emit('showRegister', flag)
    }
  }
}
</script>

<style lang='less' scoped>
  .box {
    width: 100%;
    height: auto;
    .login-form {
      height: 200px;
      border-bottom: 1px solid #c6c6c6;
      /deep/.el-input__inner, .el-button {
        width: 220px;
        height: 30px;
        line-height: 2px;
      }
      /deep/.el-button--primary {
        background-color: #0c73c2;
        border: 1px solid #0c73c2;
      }
      /deep/.el-form-item__content {
        margin-left: 0;
      }
    }
    .footer {
      height: 50px;
      .other-login-method {
        color: #0c73c2;
      }
      .free-register {
        color: #b0b0b0;
      }
    }
  }
</style>